import { legacy_createStore as createStore, applyMiddleware, compose } from 'redux';
import createSagaMiddleware from 'redux-saga';
import rootReducer from './reducers';
import rootSaga from './sagas';

const configureStore = (preloadedState?: any) => {
    const sagaMiddleware = createSagaMiddleware();
    const store = createStore(rootReducer, preloadedState, compose(applyMiddleware(sagaMiddleware)));

    if (module.hot) {
        // Enable Webpack hot module replacement for reducers
        module.hot.accept('./reducers', () => {
            store.replaceReducer(rootReducer);
        });
    }
    sagaMiddleware.run(rootSaga);
    return store;
};

export default configureStore;
